/**
 *
 */
package com.ssyt.examinationsystem.service.impl;

import com.alibaba.fastjson.JSON;
import com.ssyt.examinationsystem.dao.*;
import com.ssyt.examinationsystem.dao.impl.*;
import com.ssyt.examinationsystem.dto.PaperDto;
import com.ssyt.examinationsystem.entity.ManualEntity;
import com.ssyt.examinationsystem.entity.PaperInfo;
import com.ssyt.examinationsystem.service.IPaperInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
public class PaperInfoServiceImpl implements IPaperInfoService {

    IPaperInfoDAO paperDao = new PaperInfoDAOImpl();
    IRuleInfoDAO ruleDao = new RuleInfoDAOImpl();
    IChoiceInfoDAO choiceDao = new ChoiceInfoDAOImpl();
    IBlankInfoDAO blankDao = new BlankInfoDAOImpl();
    IJudgeInfoDAO judgeDao = new JudgeInfoDAOImpl();
    ISubjectiveInfoDAO subjectiveDao = new SubjectiveInfoDAOImpl();

    @Override
    public List<Map<String, Object>> getPapers() {
        return paperDao.getPapers();
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.ssyt.examinationsystem.service.IPaperInfoService#addPaper(com.ssyt.
     * examinationsystem.entity.PaperInfo)
     */
    @Override
    public int addPaper(PaperInfo paper) {
        return paperDao.addPaper(paper);
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.ssyt.examinationsystem.service.IPaperInfoService#deletePaper(java.
     * lang.String)
     */
    @Override
    public int deletePaper(String paperId) {
        return paperDao.deletePaper(paperId);
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.ssyt.examinationsystem.service.IPaperInfoService#updatePaper(com.ssyt
     * .examinationsystem.entity.PaperInfo)
     */
    @Override
    public int updatePaper(PaperInfo paper) {
        return paperDao.updatePaper(paper);
    }

    /**
     * 获取整个试卷预览页面所有的数据的业务方法
     */
    public PaperDto getPaperInfoById(String paperId) {
        // 创建一个存储单选题的集合对象句柄
        List<Map<String, Object>> singleList = null;
        // 创建一个存储多选题的集合对象句柄
        List<Map<String, Object>> multipleList = null;
        // 创建一个存储填空题的集合对象句柄
        List<Map<String, Object>> blankList = null;
        // 创建一个存储判断题的集合对象句柄
        List<Map<String, Object>> judgeList = null;
        // 创建一个存储主观题的集合对象句柄
        List<Map<String, Object>> subjectiveList = null;

        // 创建一个试卷Dto对象
        PaperDto dto = new PaperDto();
        Map<String, Object> paper = paperDao.getPaperInfoById(paperId);
        // 获取科目编号
        String courseId = String.valueOf(paper.get("courseId"));
        // 从paper对象中获取规则id
        String ruleId = String.valueOf(paper.get("ruleId"));
        // 通过规则id获取规则对象
        Map<String, Object> rule = ruleDao.getRuleInfoById(ruleId);
        rule.put("totalScore", Float.valueOf(String.valueOf(rule.get("totalScore"))).intValue());
        // 从paper对象中获取组卷类型
        String paperType = String.valueOf(paper.get("testId"));

        // 根据组卷类型启用不用试卷解释生成的方式
        if ("1".equals(paperType)) { // 手动

            singleList = new ArrayList<Map<String, Object>>();
            multipleList = new ArrayList<Map<String, Object>>();
            // 创建一个存储填空题的集合对象
            blankList = new ArrayList<Map<String, Object>>();

            // 创建一个存储判断题的集合对象
            judgeList = new ArrayList<Map<String, Object>>();

            // 创建一个存储主观题的集合对象
            subjectiveList = new ArrayList<Map<String, Object>>();

            ///////////////////// 单选题//////////////////
            // 获取单选的ids的json字符串
            String ids = String.valueOf(rule.get("scIds"));
            List<ManualEntity> manualList = JSON.parseArray(ids, ManualEntity.class);

            if (manualList != null && manualList.size() > 0) {
                for (ManualEntity manual : manualList) {
                    String id = manual.getId();
                    String score = manual.getScore();
                    Map<String, Object> single = choiceDao.getChoiceById(id, courseId);
                    single.put("score", score);

                    singleList.add(single);
                }
            }

            /////////////////// 多选题 //////////////////////////
            // 获取多选题json字符串
            String mulids = String.valueOf(rule.get("mulIds"));
            manualList = JSON.parseArray(mulids, ManualEntity.class);

            if (manualList != null && manualList.size() > 0) {
                for (ManualEntity manual : manualList) {
                    String id = manual.getId();
                    String score = manual.getScore();
                    Map<String, Object> multiple = choiceDao.getChoiceById(id, courseId);
                    multiple.put("score", score);

                    multipleList.add(multiple);
                }
            }

            ////////////////////// 填空题///////////////////
            // 获取多选题json字符串
            String bkIds = String.valueOf(rule.get("bkIds"));
            manualList = JSON.parseArray(bkIds, ManualEntity.class);

            if (manualList != null && manualList.size() > 0) {
                for (ManualEntity manual : manualList) {
                    String id = manual.getId();
                    String score = manual.getScore();
                    Map<String, Object> blank = blankDao.getBlankInfoById(id, courseId);
                    blank.put("score", score);

                    blankList.add(blank);
                }
            }
            ////////////////////// 判断题///////////////////
            // 获取多选题json字符串
            String judgeIds = String.valueOf(rule.get("jdIds"));
            manualList = JSON.parseArray(judgeIds, ManualEntity.class);

            if (manualList != null && manualList.size() > 0) {
                for (ManualEntity manual : manualList) {
                    String id = manual.getId();
                    String score = manual.getScore();
                    Map<String, Object> judge = judgeDao.getJudgeInfoById(id, courseId);
                    judge.put("score", score);

                    judgeList.add(judge);
                }
            }

            ////////////////////// 主观题///////////////////
            // 获取多选题json字符串
            String subIds = String.valueOf(rule.get("subIds"));
            manualList = JSON.parseArray(subIds, ManualEntity.class);

            if (manualList != null && manualList.size() > 0) {
                for (ManualEntity manual : manualList) {
                    String id = manual.getId();
                    String score = manual.getScore();
                    Map<String, Object> subjective = subjectiveDao.getSubjectiveInfoById(id, courseId);
                    subjective.put("score", score);

                    subjectiveList.add(subjective);
                }
            }

        } else { // 随机

            // 获取所有题题数
            String choiceNumber = String.valueOf(rule.get("scNumber"));
            String multipleNumber = String.valueOf(rule.get("mulNumber"));
            String blankNumber = String.valueOf(rule.get("bkNumber"));
            String judgeNumber = String.valueOf(rule.get("jdNumber"));
            String subNumber = String.valueOf(rule.get("subNumber"));

            singleList = choiceDao.getRandomChoiceById(choiceNumber, "1", courseId);
            multipleList = choiceDao.getRandomChoiceById(multipleNumber, "2", courseId);
            blankList = blankDao.getRandomBlankInfoById(blankNumber, courseId);
            judgeList = judgeDao.getRandomJudgeInfoById(judgeNumber, courseId);
            subjectiveList = subjectiveDao.getRandomSubjectiveInfoById(subNumber, courseId);
        }

        // 设置试卷对象到dto中
        dto.setPaper(paper);
        // 设置规则对象到dto中
        dto.setRule(rule);
        // 设置单选题对象集合到dto中
        dto.setSingleList(singleList);
        dto.setMultipleList(multipleList);
        dto.setBlankList(blankList);
        dto.setJudgeList(judgeList);
        dto.setSubjectiveList(subjectiveList);

        return dto;
    }

    /* (non-Javadoc)
     * @see com.ssyt.examinationsystem.service.IPaperInfoService#getPaperName(java.lang.String)
     */
    @Override
    public String getPaperName(String paperId) {
        return paperDao.getPaperName(paperId);
    }

}
